package jdk.service;



import entity.User;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class UserServiceInterceptor implements InvocationHandler {

    Object realObj;
    public UserServiceInterceptor() {
    }

    public UserServiceInterceptor(Object realObj) {
        super();
        this.realObj = realObj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        if (args!=null&&args.length>0&&args[0] instanceof User) {
            User user=(User) args[0];
            if (user.getName().trim().length() <= 1) {
                throw new RuntimeException("用户姓名输入长度需要大于1;");
            }
        }

        Object ret = method.invoke(realObj, args);
        System.out.println("操作成功");
        return ret;
    }
}
